package net.jarang.android.saleoffline.activity.sale;

import java.util.ArrayList;
import java.util.List;

import net.jarang.android.saleoffline.SaleContext;
import net.jarang.android.saleoffline.activity.AbstractBaseActivity;
import net.jarang.android.saleoffline.activity.R;
import net.jarang.android.saleoffline.domain.Sale;
import net.jarang.android.saleoffline.domain.SaleList;
import net.jarang.android.saleoffline.domain.SaleMockDao;
import net.jarang.android.saleoffline.domain.SaleSearch;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SaleListActivity extends AbstractBaseActivity implements OnItemClickListener {
	private Context context = this;
	public SaleMockDao saleDao = new SaleMockDao();
	
	private SaleList saleList;
	private List<Sale> totalSales = new ArrayList<Sale>();
	private SaleSearch saleSearch;
	private SaleListAdapter saleListAdapter;
	private boolean isFirstDraw;
	
	private ListView saleLV;
	private TextView saleMoreTV;
	
	private ProgressDialog loadindDialog;
	private EditText searchBoxET;
	private Button reloadBT;
	
	private boolean isRefreshAction;
	private int page = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sale_list);
		isFirstDraw = true;
		saleLV = (ListView) findViewById(R.id.saleLV);
		searchBoxET = (EditText) findViewById(R.id.searchBoxET);
		searchBoxET.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(context, SaleSearchActivity.class);
	        	startActivity(intent);
			}
		});
		reloadBT = (Button) findViewById(R.id.reloadIB);
		reloadBT.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				isRefreshAction = true;
				page = 1;
				totalSales.clear();
				loadData();
			}
		});
		
		loadData();
	}
	
    private void loadData() {
    	loadindDialog = ProgressDialog.show(this, null, "데이터 수신중입니다.", true, false);
    	
        Thread thread = new Thread(new Runnable() {
            public void run() {
            	try {
	            	saleSearch = saleDao.makeSaleSearch();
	        		saleList = saleDao.search(saleSearch, page);
	        		totalSales.addAll(saleList.getSales());
            	} catch (Exception e) {
            		Log.e(SaleContext.TAG, "네트워크 에러", e);
            		throw new IllegalArgumentException();
            	}
        		handler.sendEmptyMessage(0);
            }
        });
        thread.start();
    }
    
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            loadindDialog.dismiss();
            drawListView();	
        }
    };

	private void drawListView() {
		searchBoxET.setText(saleSearch.getDisplay());
		saleLV = (ListView) findViewById(R.id.saleLV);
		
		if (isFirstDraw) {
			LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
			LinearLayout lastLayout = (LinearLayout) inflater.inflate(R.layout.sale_list_row_last, null);
			saleMoreTV = (TextView)lastLayout.findViewById(R.id.listMoreViewTV);
			saleLV.addFooterView(lastLayout);

			saleListAdapter = new SaleListAdapter(this, R.layout.sale_list_row, saleList.getSales());
			saleLV.setAdapter(saleListAdapter);
			saleLV.setOnItemClickListener(this);
			isFirstDraw = false;
		} else {
			if (isRefreshAction) {
				saleListAdapter.clear();
				isRefreshAction = false;
			}
			for (Sale each : saleList.getSales()) {
				saleListAdapter.add(each);
			}
			saleListAdapter.notifyDataSetChanged();
		}
		saleMoreTV.setText(getFooterMessage());
	}

	private String getFooterMessage() {
		return "20개 더보기 " + (saleLV.getCount() - 1) + "/" + saleList.getTotalCount();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (isMoreViewAction(id)) {
			page++;
			loadData();
		} else {
			Intent intent = new Intent(this, SaleViewActivity.class);
	    	intent.putExtra("id", totalSales.get(position).getId());
	    	startActivity(intent);
		}
	}

	private boolean isMoreViewAction(long id) {
		return id < 0;
	}
}
